package xyz.scootaloo.mono.data.entity.auth

import org.ktorm.database.Database
import org.ktorm.entity.Entity
import org.ktorm.entity.sequenceOf
import org.ktorm.global.addEntity
import org.ktorm.global.findAll
import org.ktorm.schema.Table
import org.ktorm.schema.datetime
import org.ktorm.schema.int
import org.ktorm.schema.varchar
import java.time.LocalDateTime

/**
 * 角色表对应的实体类
 *
 * DAO [AuthRoles]
 *
 * @author flutterdash@qq.com
 * @since 2021/7/26 14:13
 */
interface AuthRole : Entity<AuthRole>, Comparable<AuthRole> {

    var code: Int         // 角色代码
    var name: String      // 角色的名称
    var value: Int        // 拥有的菜单项
    var desc: String      // 描述
    var status: Int       // 状态
    var created: LocalDateTime     // 创建时间
    var updated: LocalDateTime     // 修改时间

    override fun compareTo(other: AuthRole): Int {
        return this.code - other.code
    }

    companion object : Entity.Factory<AuthRole>() {
        fun edit(init: AuthRole.() -> Unit): AuthRole {
            return AuthRole {
                status = 1
                created = LocalDateTime.now()
                updated = LocalDateTime.now()
            }.apply(init)
        }
    }
}

object AuthRoles : Table<AuthRole>("auth_role") {
    private val code = int("code").primaryKey().bindTo { it.code }
    private val name = varchar("name").bindTo { it.name }
    private val value = int("value").bindTo { it.value }
    private val desc = varchar("desc").bindTo { it.desc }
    private val status = int("status").bindTo { it.status }
    private val created = datetime("created").bindTo { it.created }
    private val updated = datetime("updated").bindTo { it.updated }

    fun insert(authRole: AuthRole): Int = addEntity(authRole)
    fun list(): List<AuthRole> = findAll()
}


val Database.authRoles get() = this.sequenceOf(AuthRoles)
